package com.example.mansystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mansystem.dto.ExcelRegisterDto;
import com.example.mansystem.dto.LoginDto;
import com.example.mansystem.dto.RegisterDto;
import com.example.mansystem.dto.UserInfoDto;
import com.example.mansystem.entity.DepartmentDO;
import com.example.mansystem.entity.UserDO;
import com.example.mansystem.exception.CodeAndMsg;
import com.example.mansystem.exception.UserDefinedException;
import com.example.mansystem.mapper.DepartmentMapper;
import com.example.mansystem.mapper.RoleMapper;
import com.example.mansystem.mapper.UserMapper;
import com.example.mansystem.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserService extends ServiceImpl<UserMapper, UserDO> {

    @Resource
    UserMapper userMapper;
    @Resource
    RoleMapper roleMapper;
    @Resource
    DepartmentMapper departmentMapper;

    public void creatUser(RegisterDto dto)
    {
        UserDO user =new UserDO();
        BeanUtils.copyProperties(dto,user);
        user.setRoleid(2);
        userMapper.insert(user);
    }

    public void UserLogin(LoginDto dto)
    {
        UserDO user = this.getUserByUsername(dto.getUsername());
        if(user == null)
        {
            throw new UserDefinedException(CodeAndMsg.UNKNOW_USERNAME);
        }

        if(!Objects.equals(dto.getPassword(), user.getPassword()))
            throw new UserDefinedException(CodeAndMsg.ERROR_PASSWORD);
    }

    public List<DepartmentDO> getAllDepartment()
    {
        return departmentMapper.selectList(null);
    }

    public boolean checkExist(String username)
    {
        int count = userMapper.selectCountByUsername(username);
        if(count==0)
        {
            return false;
        }
        return true;
    }

    public UserDO getUserByUsername(String username)
    {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getUsername, username);
        return this.getOne(wrapper);
    }


    public List<String> getUserRoute(String username)
    {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getUsername, username);
        return roleMapper.getRouteByRoleId(this.getOne(wrapper).getRoleid());
    }

    public List<String> getUserApi(String username)
    {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getUsername, username);
        return roleMapper.getApiByRoleId(this.getOne(wrapper).getRoleid());
    }

    public List<UserDO> getAllUser()
    {
        return userMapper.selectList(null);
    }

    public void UpdateUser(UserInfoDto dto)
    {
        if(dto.getId()==1)
            throw new UserDefinedException(CodeAndMsg.AdminCantChange);
        userMapper.updateUser(dto.getId(),dto.getName(),dto.getRoleid(),dto.getSex(),dto.getUsername(),dto.getDepartmentId());
    }

    public void ExcelRegister(ExcelRegisterDto dto)
    {
        for(int i=0;i<dto.getExcelRegister().size();i++)
        {
            UserDO user =new UserDO();
            BeanUtils.copyProperties(dto.getExcelRegister().get(i),user);
            userMapper.insert(user);
        }

    }

}
